layui.use(['table', 'layer', 'form', 'laydate'], function () {
    var layer = layui.layer;
    var table = layui.table;
    var form = layui.form;
    var laydate = layui.laydate;
    table.render({
        elem: '#table1',
        url: '/order/getOrderInfo',
        method: 'post',
        cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
        cols: [[
            {field: 'id', title: 'ID', align: 'center', type: 'numbers'},
            {field: 'orderid', title: '订单号', align: 'center'},
            {field: 'startaddr', title: '订单起点', align: 'center'},
            {field: 'endaddr', title: '订单终点', align: 'center'},
            {field: 'sendername', title: '寄件人姓名', align: 'center'},
            {field: 'rname', title: '收件人姓名', align: 'center'},
            {field: 'rephone', title: '收件人电话', align: 'center'},
            {field: 'orderclass', title: '订单类别', align: 'center'},
            {field: 'action1', title: '操作', align: 'center', toolbar: '#bar1'}
        ]],
        page: true
    });
    form.on('submit(osub)',function (data) {
        var otip=data.field.otip;
        table.render({
            elem: '#table1',
            url: '/order/getOrderInfo?otip='+otip,
            method: 'post',
            cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            cols: [[
                {field: 'id', title: 'ID', align: 'center', type: 'numbers'},
                {field: 'orderid', title: '订单号', align: 'center'},
                {field: 'startaddr', title: '订单起点', align: 'center'},
                {field: 'endaddr', title: '订单终点', align: 'center'},
                {field: 'sendername', title: '寄件人姓名', align: 'center'},
                {field: 'rname', title: '收件人姓名', align: 'center'},
                {field: 'rephone', title: '收件人电话', align: 'center'},
                {field: 'orderclass', title: '订单类别', align: 'center'},
                {field: 'action1', title: '操作', align: 'center', toolbar: '#bar1'}
            ]],
            page: true
        });
    });
    form.on('submit(ao)', function (data) {
        console.log(data.field);
        data.field.senderaddr = data.field.spro + ' ' + data.field.scity + ' ' + data.field.sdis;
        data.field.qujianaddr = data.field.qpro + ' ' + data.field.qcity + ' ' + data.field.qdis;
        data.field.readdr = data.field.rpro + ' ' + data.field.rcity + ' ' + data.field.rdis;
        data.field.startaddr = data.field.qcity;
        data.field.endaddr = data.field.rcity;
        data.field.orderstatus = "已审核";
        $.ajax({
            type: 'post',
            url: '/order/addOrderByBack',
            data: JSON.stringify(data.field),
            contentType: 'application/json; charset=utf-8',
            success: function (result) {
                if (result == 'success') {
                    $("#complete").empty();
                    $("#complete").append("<p style='text-align: center;margin-right: 75px;color: seagreen;font-size: 20px;'><i class='layui-icon layui-icon-face-smile' style='color: #00ba6d;font-size: x-large;'></i>上传成功！</p>");
                    // layer.msg('添加成功,3s后自动关闭',{icon:1});
                } else {
                    $("#complete").empty();
                    $("#complete").append("<p style='text-align: center;margin-right: 75px;color: #ff4100;font-size: 20px;'><i class='layui-icon layui-icon-face-cry' style='color: #d43100;font-size: x-large;'></i>上传失败，请重试！</p>");
                    // layer.msg('添加成功,3s后自动关闭',{icon:1});
                    // layer.msg('添加失败,3s后自动关闭',{icon:2});
                }
            },
            error: function (error) {
                layer.msg('404,error', {icon: 2});
            }
        });
        return false;
    });
    //编辑,删除表格行数据
    table.on('tool(table1)', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        console.log(data.id);
        if (layEvent == "edit") {
            layer.open({
                type: 2,
                title: '修改订单',
                shadeClose: false,
                shade: 0.8,
                maxmin: true,
                area: ['65%', '90%'],
                content: '/order/editOrderInfo?id=' + data.id
            });
        }
        if (layEvent == "del") {
            layer.confirm("确认删除？", function () {
                $.ajax({
                    type: 'post',
                    url: '/order/delOrderInfo',
                    data: {
                        "id": data.id
                    },
                    success: function (result) {
                        if (result == "success") {
                            layer.msg("删除成功！", {icon: 1, time: 1000}, function (index) {
                                layer.close(index);
                                location.reload();
                            });
                        }
                        if (result == "fail") {
                            layer.msg("删除失败！", {icon: 2, time: 1000}, function (index) {
                                layer.close(index);
                            });
                        }
                        if (result == "infoError") {
                            layer.msg("错误请重试！", {icon: 2, time: 1000}, function (index) {
                                layer.close(index);
                            });
                        }
                    },
                    error: function (error) {
                        layer.msg("404");
                    }
                });
            })
        }
        if (layEvent == "detail") {
            layer.open({
                type: 2,
                title: '订单详情',
                shadeClose: false,
                shade: 0.8,
                maxmin: true,
                area: ['60%', '100%'],
                content: '/order/orderInfoDtl?id=' + data.id
            });
        }
    });
    form.on('submit(edito)', function (data) {
        //补充数据
        data.field.senderaddr = data.field.spro + ' ' + data.field.scity + ' ' + data.field.sdis;
        data.field.qujianaddr = data.field.qpro + ' ' + data.field.qcity + ' ' + data.field.qdis;
        data.field.readdr = data.field.rpro + ' ' + data.field.rcity + ' ' + data.field.rdis;
        data.field.startaddr = data.field.qcity;
        data.field.endaddr = data.field.rcity;
        console.log(data.field);
        $.ajax({
            type: 'post',
            url: '/order/editOrder',
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(data.field),
            success: function (result) {
                if (result == "info-error") {
                    layer.alert("上传失败！", {icon: 2}, function (index) {
                        layer.close(index);
                    });
                }
                if (result == "success") {
                    layer.alert("修改成功！", {icon: 1}, function (index) {
                        layer.close(index);
                        var pIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(pIndex);
                        parent.location.reload();
                    });
                }
                if (result == "fail") {
                    layer.alert("修改失败，请重试！", {icon: 2}, function (index) {
                        layer.close(index);
                    });
                }
            },
            error: function (error) {
                layer.msg("404");
            }
        });
        return false;
    });

    /*ps_info*/
    table.render({
        elem: '#tablep',
        url: '/order/getPSInfo',
        method: 'post',
        cellMinWidth: 125, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
        cols: [[
            {field: 'id', title: 'ID', align: 'center', type: 'numbers'},
            {field: 'psbh', title: '配送编号', align: 'center'},
            {field: 'jjrxm', title: '寄件人姓名', align: 'center'},
            {field: 'sjrxm', title: '收件人姓名', align: 'center'},
            {field: 'sjrdh', title: '收件人电话', align: 'center'},
            {field: 'ddlb', title: '订单类别', align: 'center'},
            {field: 'saddr', title: '起点', align: 'center'},
            {field: 'eaddr', title: '终点', align: 'center'},
            {field: 'pswp', title: '配送物品', align: 'center'},
            {field: 'clbh', title: '车牌编号', align: 'center'},
            {field: 'cphm', title: '车牌号码', align: 'center'},
            {field: 'sjxm', title: '司机姓名', align: 'center'},
            {field: 'sjdh', title: '司机电话', align: 'center'},
            {field: 'pstime', title: '发货时间', align: 'center'},
            {field: 'shtime', title: '送达时间', align: 'center'},
            {field: 'zt', title: '状态', align: 'center'},
            {field: 'action1', title: '操作', align: 'center', toolbar: '#bar1'}
        ]],
        page: true
    });
    form.on('submit(psub)', function (data) {
        var pstip = data.field.pstip;
        table.render({
            elem: '#tablep',
            url: '/order/getPSInfo?pstip='+pstip,
            method: 'post',
            cellMinWidth: 125, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            cols: [[
                {field: 'id', title: 'ID', align: 'center', type: 'numbers'},
                {field: 'psbh', title: '配送编号', align: 'center'},
                {field: 'jjrxm', title: '寄件人姓名', align: 'center'},
                {field: 'sjrxm', title: '收件人姓名', align: 'center'},
                {field: 'sjrdh', title: '收件人电话', align: 'center'},
                {field: 'ddlb', title: '订单类别', align: 'center'},
                {field: 'saddr', title: '起点', align: 'center'},
                {field: 'eaddr', title: '终点', align: 'center'},
                {field: 'pswp', title: '配送物品', align: 'center'},
                {field: 'clbh', title: '车牌编号', align: 'center'},
                {field: 'cphm', title: '车牌号码', align: 'center'},
                {field: 'sjxm', title: '司机姓名', align: 'center'},
                {field: 'sjdh', title: '司机电话', align: 'center'},
                {field: 'pstime', title: '发货时间', align: 'center'},
                {field: 'shtime', title: '送达时间', align: 'center'},
                {field: 'zt', title: '状态', align: 'center'},
                {field: 'action1', title: '操作', align: 'center', toolbar: '#bar1'}
            ]],
            page: true
        });

        return false;
    });
    //编辑,删除表格行数据
    table.on('tool(tablep)', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        console.log(data);
        // if (layEvent == "edit") {
        //     layer.open({
        //         type: 2,
        //         title: '修改订单',
        //         shadeClose: false,
        //         shade: 0.8,
        //         maxmin: true,
        //         area: ['65%', '90%'],
        //         content: '/order/editOrderInfo?id=' + data.id
        //     });
        // }
        if (layEvent == "del") {
            layer.confirm("确认删除？", function () {
                $.ajax({
                    type: 'post',
                    url: '/order/delPSInfo',
                    data: {
                        "psbh": data.psbh
                    },
                    success: function (result) {
                        if (result == "success") {
                            layer.msg("删除成功！", {icon: 1, time: 1000}, function (index) {
                                layer.close(index);
                                location.reload();
                            });
                        }
                        if (result == "fail") {
                            layer.msg("删除失败！", {icon: 2, time: 1000}, function (index) {
                                layer.close(index);
                            });
                        }
                    },
                    error: function (error) {
                        layer.msg("404");
                    }
                });
            })
        }
        if (layEvent == "sh") {
            layer.confirm("确认送达？", function () {
                $.ajax({
                    type: 'post',
                    url: '/order/shPS',
                    data: {
                        "psbh": data.psbh,
                        "clbh": data.clbh,
                        "sjxm": data.sjxm,
                        "sjdh": data.sjdh
                    },
                    success: function (result) {
                        if (result == "success") {
                            layer.msg("配送完成！", {icon: 1, time: 1000}, function (index) {
                                layer.close(index);
                                location.reload();
                            });
                        }
                        if (result == "p") {
                            layer.msg("配送单更改失败，请手动修改！", {icon: 2, time: 1000}, function (index) {
                                layer.close(index);
                            });
                        }
                        if (result == "c") {
                            layer.msg("车辆信息更改失败，请手动修改！", {icon: 2, time: 1000}, function (index) {
                                layer.close(index);
                            });
                        }
                        if (result == "d") {
                            layer.msg("司机信息更改失败，请手动修改！", {icon: 2, time: 1000}, function (index) {
                                layer.close(index);
                            });
                        }
                        if (result == "pc") {
                            layer.msg("配送单和车辆信息更改失败，请手动修改！", {icon: 2, time: 1000}, function (index) {
                                layer.close(index);
                            });
                        }
                        if (result == "pd") {
                            layer.msg("配送单和司机信息更改失败，请手动修改！", {icon: 2, time: 1000}, function (index) {
                                layer.close(index);
                            });
                        }
                        if (result == "cd") {
                            layer.msg("车辆信息和司机信息更改失败，请手动修改！", {icon: 2, time: 1000}, function (index) {
                                layer.close(index);
                            });
                        }
                        if (result == "pcd") {
                            layer.msg("配送单、车辆信息和司机信息更改失败，请手动修改！", {icon: 2, time: 1000}, function (index) {
                                layer.close(index);
                            });
                        }
                    },
                    error: function (error) {
                        layer.msg("404");
                    }
                });
            });
        }
    });
});
$(function () {
    $("#addOrder").click(function () {
        location.href = '/order/addOrderInfo';
    });
});